import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/user/Login'
import store from '@/store/index'
import HomeLayout from '@/layouts/HomeLayout'
Vue.use(VueRouter)

const routes = [
  {
    path:"/login",
    name:'Login',
    component:Login,
  }, 
  {
    path:"/",
    name:'HomeLayout',
    component:HomeLayout,
    children:[
      
      {
        path:'/welcome',
        name:'Welcome',
        component: () =>import ('@/views/other/Welcome')
      },
      {
        path:'/users',
        name:'Users',
        component: () =>import ('@/views/user/List')
      },
      {
        path:'/roles',
        name:'RoleList',
        component: () =>import ('@/views/rights/RoleList')
      },
      {
        path: '/rights',
        name: 'RightsList',
        component: () => import('@/views/rights/RightsList')
      },
      {
        path: '/goods',
        name: 'List',
        component: () => import('@/views/goods/List')
      },
      {
        path: '/params',
        name: 'Params',
        component: () => import('@/views/goods/Params')
      },
      {
        path: '/categories',
        name: 'Categories',
        component: () => import('@/views/goods/Categories')
      },
      {
        path: '/orders',
        name: 'Orders',
        component: () => import('@/views/order/List')
      },
      {
        path: '/reports',
        name: 'Reports',
        component: () => import('@/views/reports/List')
      },
      {
        path:'*',
        name:'404',
        component: () =>import ('@/views/other/404')
      }
    ]
  },
]

const router = new VueRouter({
  mode:'history',
  base:process.env.BASE_URL,
  routes
})

/*l
 * 路由守卫
 */
router.beforeEach((to,form,next) => {
  //如果是登录页，并且有token，就进入首页。
  // console.log(to.name)
  if(to.name === 'Login' && store.getters.token){
    return next({name: 'HomeLayout' });
  }
  //如果不是，没有token，就进入登录页。
  if(to.name !== 'Login' && !store.getters.token){
    return next({name:'Login'});
  }
  next();
})

export default router
